package game.module.battleReport.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.module.battleReport.bean.BattleReport;
import game.module.battleReport.bean.PlayerReport;

public class BattleReportCache {

	private static Logger logger = LoggerFactory.getLogger(BattleReportCache.class);

	static class SingletonHolder {
		static BattleReportCache instance = new BattleReportCache();
	}

	public static BattleReportCache getInstance() {
		return SingletonHolder.instance;
	}

	public Map<Long, BattleReport> realreportcache = new ConcurrentHashMap<Long, BattleReport>();

	public Map<Long, Set<Integer>> battleNumcache = new ConcurrentHashMap<Long, Set<Integer>>();

	public void playerGetReport(BattleReport battle, int playerId) {

	}

	/**/
	public void addBattleReportInCache(long battleid, int playerId) {
		if (realreportcache.containsKey(battleid)) {
			Set<Integer> players = battleNumcache.get(battleid);
			players.add(playerId);
			return;
		}
		BattleReport battlereport = BattleReportDBDao.getInstance().getRealbattle(battleid);
	    if(battlereport==null)
	    	return;
		Set<Integer> players = new HashSet<Integer>();
		players.add(playerId);
		battleNumcache.put(battleid, players);
		realreportcache.put(battleid, battlereport);


	}

	/**
	 * 删除
	 **/
	public void playerDownline(int playerId) {
		List<PlayerReport> playerreports = PlayerReportCache.getInstance().getBattleReport(playerId);
		if (playerreports!=null&&playerreports.size() > 0) {
			for (PlayerReport areport : playerreports) {
				// areport.get
				Set<Integer> plays = battleNumcache.get(areport.getBattleperortId());
				Integer playobj = new Integer(playerId);
				plays.remove(playobj);
				if (plays.size() == 0) {
					battleNumcache.remove(areport.getBattleperortId());
					realreportcache.remove(areport.getBattleperortId());
					logger.info("player down line remove battlereport");
				}
			}
		}

	}

	/**
	 * 获得
	 */
	public BattleReport getRealReport(long reportid) {
		return realreportcache.get(reportid);
	}
}
